import json


class Data:
    def __init__(self, name, age, city):
        self.name = name
        self.age = age
        self.city = city


def serialize(obj):
    # json.dumps 将对象序列化为json字符串
    return json.dumps(obj.__dict__)


def deserialize(json_str, obj):
    # json.loads 将json字符串反序列化为字典对象
    data = json.loads(json_str)
    obj.name = data['name']
    obj.age = data['age']
    obj.city = data['city']
    return obj


# 创建一个数据对象
data = Data('John', 30, 'New York')

# 序列化数据
json_str = serialize(data)
print('Serialized:', json_str)
print('Serialized type:', type(json_str))

# 反序列化数据
new_data = deserialize(json_str, Data('', 0, ''))
print('Deserialized:', new_data.__dict__)
print('Deserialized type:', type(new_data.__dict__))
